<?php

/**
 * Created by PhpStorm.
 * User: shwy
 * Date: 2017/8/8
 * Time: 11:20
 * 七牛云上传类
 */
require_once './application/third_party/qiniu/autoload.php';

use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
class  Qiniu
{

    // 默认为procurement空间,如果有其他的则传入其他的

    public function upload($filePath,$key){
        $CI  =& get_instance();

        // 用于签名的公钥和私钥
//        $accessKey = 'WFL-Yl8a1WoQt0UfqWfQxpgly2Xyd9LwOlb5XXmh';

        $accessKey = $CI->config->item('qiniu')['accessKey'];
        $secretKey = $CI->config->item('qiniu')['secretKey'];
        $bucket = $CI->config->item('qiniu')['bucket'];

        // 初始化签权对象
        $auth = new Auth($accessKey, $secretKey);

        // 空间名  https://developer.qiniu.io/kodo/manual/concepts
        // 生成上传 Token
        $token = $auth->uploadToken($bucket);




        // 要上传文件的本地路径
//        $filePath = $_FILES['test']['tmp_name'];

        // 上传到七牛后保存的文件名
//        $key = 'my-php-2logo.png';
        // 初始化 UploadManager 对象并进行文件的上传
        $uploadMgr = new UploadManager();
        // 调用 UploadManager 的 putFile 方法进行文件的上传
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);

        return $err;

//        echo "\n====> putFile result: \n";
        if ($err !== null) {
            print_r($err);
        } else {
            print_r($ret);
        }



    }



}










































